* ======================================
* --------- Import population ----------
* ======================================
* Create vote-eligible population as per Sept 1 in 2015 and Sept 1 in 2019


	use $dta\ImportPers.dta, clear

	
* 2015
* ====
	preserve
	gen year = 2015

	keep if birthyear <= 2015 - 18
	keep if deathyear >= 2015 
	drop if deathyear == 2015 & deathmonth < 9 	// passed away before Sept '15
	
	merge 1:1 idPers year using "$dta\ImportGeo.dta", nogen ///
		keep(matched) // drop if no address in Norway
	
	tempfile pop2015
	save `pop2015'
	restore


* 2019
* ====
	preserve
	gen year = 2019
	keep if birthyear <= 2019 - 18
	keep if deathyear >= 2019 
	drop if deathyear == 2019 & deathmonth < 9 	// passed away before Sept '19
	
	merge 1:1 idPers year using "$dta\ImportGeo.dta", nogen ///
		keep(matched) // drop if no address in Norway
	tempfile pop2019
	save `pop2019'
	restore
	
	
* Append in long format
* =====================
	
	clear
	append using `pop2015'
	append using `pop2019'
	gen age = year - birthyear

	keep idPers year birthyear age birthcountry female knr bsu immigrant
	order idPers year birthyear age birthcountry immigrant female knr bsu
	
	save "$dta\Population.dta", replace